﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_allergy_info_detail : AdminBasePage
{
    private static readonly string TAG_LAYERIDS = OgilvyOne.Utility.ConfigUtil.GetStringValue("ArticleTagLayerids");
    protected string cid;
    protected string top1aid;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                this.infoname.Text = "新增内容";
                this.headers.Text = new ColumnBll().LayeridColumnConNaiv(Layerid);

                int columnid = int.Parse(Request.Params["columnid"]);
                ColumnModel column = new ColumnBll().GetColumn(columnid);

                this.isstaticpub.Value = column.Isstaticpub.ToString();


                //初始模板
                ModelManageBLL mmb = new ModelManageBLL();
                DataSet temdata = mmb.GetModel("");
                for (int i = 0; i < temdata.Tables.Count; i++)
                {
                    DataTable table = temdata.Tables[i];
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        DataRow row = table.Rows[j];

                        this.templateId.Items.Add(new ListItem(row["tem_name"].ToString(), row["tem_id"].ToString()));

                    }
                }
                //初始专家
                //IList expertdatalist = new HealthExpertBll().GetHealthExpertList(null);
                //for (int i = 0; i < expertdatalist.Count; i++)
                //{
                //    HealthExpertModel hem = (HealthExpertModel)expertdatalist[i];
                //    expertlist.Items.Add(new ListItem(hem.ExpertName, hem.ExpertId.ToString()));
                //}

                //if (isExist(Layerid, TAG_LAYERIDS))
                //{
                //    this.contag.Visible = true;
                //}

                //this.sub3.Visible = false;
                //this.sub4.Visible = false;
                //this.sub5.Visible = false;
                if (Request.Params["id"] != null && Request.Params["id"] != "")
                {//修改
                    this.infoname.Text = "修改内容";
                    CmsArticleModel cm = new CmsArticleBll().GetCmsArticleModel(int.Parse(Request.Params["id"]));
                    this.title.Text = cm.Title;
                    this.summary.Text = cm.Summary;
                    this.content.Text = cm.Content;
                    this.indextop.Text = cm.Indextop;
                    this.sorttop.Text = cm.Sorttop;
                    this.titleImg.Value = cm.Titleimg;
                    this.state.Value = cm.Docstate;
                    this.aid.Value = cm.Articleid.ToString();
                    this.templateId.Text = cm.Templateid.ToString();
                    this.isstaticpub.Value = cm.Isstaticpub;
                    this.articletype.Text = cm.Articletype;
                    this.source.Text = cm.Source;
                    this.sourceurl.Text = cm.Sourceurl;
                    this.linkpath.Text = cm.Linkpath;
                    this.author.Text = cm.Author;
                    DataTable dt = BLLArticleInfo.selArticle(Request.Params["id"]);
                    foreach (DataRow row in dt.Rows)
                    {
                        this.doknow.SelectedValue = row["doKnow"].ToString();
                        this.recommend.SelectedValue = row["recommend"].ToString();
                        this.banner.SelectedValue = row["isbanner"].ToString();
                    }
                    if (cm.ExpertId > 0)
                    {
                        this.expertid.Value = cm.ExpertId.ToString();
                        //this.expertlist.Text = cm.ExpertId.ToString();
                    }
                    //需加对应TAG NAME
                    this.tag.Value = cm.Tag;
                    if (cm.Tag != null && cm.Tag != "")
                    {
                        IList taglist = new CmsArticleTagBll().GetTagList(cm.Tag);
                        for (int i = 0; i < taglist.Count; i++)
                        {
                            CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                            this.tagname.Text += catm.TagName + ",";
                        }
                    }
                    //this.sub3.Visible = true;

                    if (column.Isauditing == 1)
                    {
                        //if (cm.Docstate != "2") sub3.Visible = false;
                        if (this.PopedomValidateHtml(Request.Params["layerid"], AUDIT))
                        {
                            if (cm.Docstate == "0")
                            {//正稿可以审核
                                //this.sub4.Visible = true;
                                //this.sub5.Visible = true;
                            }
                        }
                        if (Request.Params["isaudit"] != null && Request.Params["isaudit"] == "1")
                        {//审核文章
                            this.sub1.Visible = false;
                            this.sub2.Visible = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(string.Format( "Error occurred in Page_Load. Message: {0}", ex.Message));
            }

        }
    }

    protected void Audi_click(object sender, EventArgs e)
    {
        this.PopedomValidate(Request.Params["layerid"], AUDIT);
        new CmsArticleBll().Audit(int.Parse(this.aid.Value), this.state.Value, GetSessionUsersModel().Username);
        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);
    }

    protected void Sub_click(object sender, EventArgs e)
    {
        int aid = 0;
        if (this.aid.Value!=null && this.aid.Value != "")
        {
            aid = int.Parse(this.aid.Value);
        }

        CmsArticleModel cm = new CmsArticleModel();
        cm.Title = this.title.Text;
        cm.Summary = this.summary.Text;
        cm.Content = this.content.Text;
        cm.Indextop = this.indextop.Text;
        cm.Sorttop = this.sorttop.Text;
        //cm.Titleimg = this.titleImg.Value; 相关图片 维护
        cm.Docstate = this.state.Value;
        cm.Templateid = int.Parse(this.templateId.Text);
        cm.Isstaticpub = this.isstaticpub.Value;

        cm.Articletype = this.articletype.Text;
        cm.Source = this.source.Text;
        cm.Sourceurl = this.sourceurl.Text;
        if (this.articletype.Text == "3")
        {//文件
            cm.Linkpath = this.filedown.Value;
        }
        else
        {//链接
            cm.Linkpath = this.linkpath.Text;
        }
        cm.Author = this.author.Text;
        cm.Tag = this.tag.Value;

        if (this.expertid.Value != null && this.expertid.Value != "")
        {
            cm.ExpertId = int.Parse(this.expertid.Value);
        }

        cm.Columnid = int.Parse(Request.Params["columnid"]);

        cm.Addusername = this.GetSessionUsersModel().Username;
        cm.Modifyusername = this.GetSessionUsersModel().Username;
        cm.Recommend = this.recommend.SelectedValue;
        cm.DoKnow = this.doknow.SelectedValue;
        cm.Isbanner = this.banner.SelectedValue;
        string param = Request.Params["columnid"];
        if (param == "180" || param == "196" || param == "204")
        {
            DataTable dt2 = BLLArticleInfo.selEnamee(param);
            cm.Parentename = dt2.Rows[0]["ECLASSNAME"].ToString();
            cm.Childenamel = dt2.Rows[0]["ECLASSNAME"].ToString();
        }
        else
        {
            DataTable dt1 = BLLArticleInfo.selEnamee(param);
            cm.Parentename = dt1.Rows[0]["ECLASSNAME"].ToString();
            cm.Childenamel = dt1.Rows[1]["ECLASSNAME"].ToString();
        }
        if (aid > 0)
        {//修改
            this.PopedomValidate(Request.Params["layerid"], MOD);
            cm.Articleid = aid;
            cm.Recommend = this.recommend.SelectedValue;
            cm.DoKnow = this.doknow.SelectedValue;
            cm.Isbanner = this.banner.SelectedValue;
            BLLArticleInfo.updataArticle(this.recommend.SelectedValue,this.doknow.SelectedValue,this.banner.SelectedValue,DateTime.Now.ToString(),aid.ToString());
            CmsArticleBll cabll= new CmsArticleBll();
            bool r = cabll.Update(cm);
            if (r)
            {
                insertLog("修改文章:" + this.title.Text);
                if (this.state.Value == "1")
                {
                    cabll.Pubone(aid, GetSessionUsersModel().Username);
                    insertLog("发布文章:" + this.title.Text);
                }
            }
        }
        else
        {
            this.PopedomValidate(Request.Params["layerid"], ADD);
            if (new CmsArticleBll().Add(cm))
            {

                insertLog("新增文章:" + this.title.Text);

            }

        }

        /*
         * 代码注释：
         * 注释日期：2012-01-07
         * 注释人员：刘扬、陈鹏
         * 注释原因：经过代码分析，以下代码为程序升级时为数据库中已有数据添加新增字段值时使用，现已无必要继续执行
         * */
        //int rowIndex = -1;
        //try
        //{
        //    DataTable alldt = BLLArticleInfo.selArticle();
        //    foreach (DataRow row in alldt.Rows)
        //    {
        //        rowIndex++;
        //        string artid = row["ARTICLEID"].ToString();
        //        string cid = row["COLUMNID"].ToString();
        //        if (cid == "180" || cid == "196" || cid == "204")
        //        {
        //            DataTable dt3 = BLLArticleInfo.selEnamee(cid);
        //            string p = dt3.Rows[0]["ECLASSNAME"].ToString();
        //            string c = dt3.Rows[0]["ECLASSNAME"].ToString();
        //            BLLArticleInfo.updataEname(p, c, artid);
        //        }
        //        else
        //        {
        //            DataTable alldt1 = BLLArticleInfo.selEnamee(cid);
        //            string p = alldt1.Rows[0]["ECLASSNAME"].ToString();
        //            string c = alldt1.Rows[1]["ECLASSNAME"].ToString();
        //            BLLArticleInfo.updataEname(p, c, artid);

        //        }
        //    }
        //}
        //catch (Exception ex)
        //{
        //    string s = ex.Message;
        //}


        Response.Redirect("info_list.aspx?columnid=" + Request.Params["columnid"] + "&layerid=" + Request.Params["layerid"], true);

    }

    protected void autotag_onclick(object sender, EventArgs e)
    {
        if (this.content.Text != "")
        {
            this.tagname.Text = "";
            this.tag.Value = "";
            CmsArticleTagModel q = new CmsArticleTagModel();
            q.State = 1;
            IList taglist = new CmsArticleTagBll().GetTagList(q);
            for (int i = 0; i < taglist.Count; i++)
            {
                CmsArticleTagModel catm = (CmsArticleTagModel)taglist[i];
                if (this.content.Text.IndexOf(catm.TagName) > -1)
                {
                    this.tag.Value += catm.TagId + ",";
                    this.tagname.Text += catm.TagName + ",";
                }
            }
        }

    }


    protected void articletype_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList rbl = (RadioButtonList)sender;
        this.down.Visible = false;
        this.link.Visible = false;
        this.con.Visible = false;
        if (rbl.Text == "3")
        {//文件
            this.down.Visible = true;
        }
        else if (rbl.Text == "2")
        {//链接
            this.link.Visible = true;

        }
        else
        {
            this.con.Visible = true;
        }
    }
    //protected void expertlist_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    if (this.expertlist.SelectedItem.Text != "请选择")
    //        this.author.Text = this.expertlist.SelectedItem.Text;
    //    else
    //        this.author.Text = "";

    //    this.expertid.Value = this.expertlist.SelectedValue;
    //}
}
